<template>
	<view class="content">
		<view class="title">
			UDI解析
		</view>
		<view>
			<view class="t">
				电脑IP
			</view>
			<view class="d">
				<input type="text" focus v-model="ip">
			</view>
		</view>
		<view class="info">
			<view class="t">
				01-产品
			</view>
			<view class="d">
				{{ udi.Di }}
			</view>
		</view>
		<view class="info">
			<view class="t">
				10-生产批号
			</view>
			<view class="d">
				{{ udi.BatchNumber }}
			</view>
		</view>
		<view class="info">
			<view class="t">
				11-生产日期
			</view>
			<view class="d">
				{{ udi.ProductionDate }}
			</view>
		</view>
		<view class="info">
			<view class="t">
				17-失效日期
			</view>
			<view class="d">
				{{ udi.ExpirationDate }}
			</view>
		</view>
		<view class="info">
			<view class="t">
				21-序列号
			</view>
			<view class="d">
				{{ udi.SerialNumber }}
			</view>
		</view>
	</view>
</template>


<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
	export default {
		data() {
			return {
				udi: {},
				ip: "192.168.1.10"
			}
		},
		created: function(option) {
			this.initScan()
			this.startScan();
		},
		onHide: function() {
			// this.stopScan();
		},
		destroyed: function() {
			/**页面退出时一定要卸载监听,否则下次进来时会重复，造成扫一次出2个以上的结果**/
			this.stopScan();
		},
		methods: {
			initScan() {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				// filter.addAction("com.android.server.scannerservice.broadcast"); // 换你的广播动作
				filter.addAction("com.gm.scanAction"); // 换你的广播动作
				filter.addCategory("com.gm.scanCate");
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						// let code = intent.getStringExtra("scannerdata"); // 换你的广播标签
						let code = intent.getStringExtra("com.symbol.datawedge.data_string"); // 换你的广播标签
						_this.queryCode(code);
					}
				});
			},
			startScan() {
				main.registerReceiver(receiver, filter);
			},
			stopScan() {
				main.unregisterReceiver(receiver);
			},
			queryCode(code) {
				//防重复
				// if (_codeQueryTag) return false;
				// _codeQueryTag = true;
				// setTimeout(function() {
				// 	_codeQueryTag = false;
				// }, 150);

				this.udi = {}
				this.udi.code = code;
				let replaced = code.replace(/[\u0000-\u001f\b\r\n]/g, ",");
				console.log(replaced);
				let list = replaced.split(',');
				for (var i = 0; i < list.length; i++) {
					this.AnalysisUDI(this.udi, list[i]);
				}

				// if (!code.startsWith("01")) {
				if (!this.udi.Di) {
					uni.showToast({
						title: '无效UDI',
						duration: 2000,
						icon: "error"
					});
				} else {
					uni.request({
						url: `http://${this.ip}:5110/udi`,
						method: "POST",
						data: this.udi,
						success: (res) => {

						},
						fail: (err) => {

						}
					})

				}
				console.log(code);
			},
			AnalysisUDI(udi, code) {

				if (!code) return; // 没有可以解析的 直接return

				let nextCode;
				if (code.startsWith("01")) {
					if (code.length <= 16) {
						udi.Di = code.slice(2);
						return;
					} else {
						udi.Di = code.slice(2, 2 + 14);
						nextCode = code.slice(2 + udi.Di.length);
						this.AnalysisUDI(udi, nextCode);
					}
				}

				if (code.startsWith("10")) {
					udi.BatchNumber = code.slice(2);
					return;
				}

				if (code.startsWith("11")) {
					udi.ProductionDate = code.slice(2, 2 + 6);
					nextCode = code.slice(8);
					this.AnalysisUDI(udi, nextCode);
				}

				if (code.startsWith("17")) {
					udi.ExpirationDate = code.slice(2, 2 + 6);
					nextCode = code.slice(8);
					this.AnalysisUDI(udi, nextCode);
				}

				if (code.startsWith("21")) {
					udi.SerialNumber = code.slice(2);
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #efeff4;
		height: 100vh;
		padding: 6px;
	}

	.content {
		text-align: center;
		background-color: white;
		height: 100%;
		border-radius: 6px;
	}

	.title {
		padding-top: 40px;

	}

	.info {
		margin: 20px;
		display: flex;
		justify-content: space-between;
	}
</style>